﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Data.Common.QueryBuilder
{
    public class DbQuerySelect
    {
        public DbQueryWhere Where
        {
            get
            {
                if (this._Where == null)
                    this._Where = new DbQueryWhere(this);
                return this._Where;
            }
        }
        private DbQueryWhere _Where;

        public DbQueryGroupBy GroupBy
        {
            get
            {
                if (this._GroupBy == null)
                    this._GroupBy = new DbQueryGroupBy(this);

                return this._GroupBy;
            }
        }
        private DbQueryGroupBy _GroupBy;

        public DbQueryOrderBy OrderBy
        {
            get
            {
                if (this._OrderBy == null)
                    this._OrderBy = new DbQueryOrderBy(this);

                return this._OrderBy;
            }
        }
        private DbQueryOrderBy _OrderBy;


        public String TableName;
        public Boolean TableInitialized
        {
            get
            {
                return String.IsNullOrEmpty(this.TableName) == false;
            }
        }
        protected List<String> Columns;
        protected List<QJoin> Joins;
        protected DbQuerySelect SelectScope;

        public DbQuerySelect()
        {
            this.Columns = new List<String>();
            this.Joins = new List<QJoin>();
        }

        public DbQuerySelect(String Table)
            : this()
        {
            this.TableName = Table;
        }
        /*
        public QSelect(QSelect scope)
            : this()
        {
            this.SelectScope = scope;
        }

        public QSelect(QSelect scope, String tableOrAlias)
            : this(tableOrAlias)
        {
            this.SelectScope= scope;
        }
        */

        public DbQuerySelect OnTable(String table)
        {
            this.TableName = table;
            return this;
        }

        public QJoin Join(String table, QJoin.JoinType joinType)
        {
            var j = new QJoin(this, table, joinType);
            this.Joins.Add(j);
            return j;
        }

        public virtual DbQuerySelect AddColumn(params String[] columns)
        {
            this.Columns.AddRange(columns);
            return this;
        }

        public override string ToString()
        {
            String envNewLine = String.Concat(Environment.NewLine, "\t");
            String joins = this.Joins.Count > 0 ? String.Concat(String.Join(Environment.NewLine, this.Joins), Environment.NewLine) : String.Empty;
            return String.Concat(
                "SELECT", String.Concat(Environment.NewLine, "\t "),
                    String.Join(String.Concat(Environment.NewLine, "\t,"), this.Columns), Environment.NewLine,
                "FROM ", this.TableName, Environment.NewLine,
                joins,
                this.GroupBy, Environment.NewLine,
                this.OrderBy);
        }
        /*
        public QSelect Scope(String ScopeName)
        {
            this.SelectScope = new QSelect(this);

            return this.SelectScope;
        }
        */
    }
}
